<?php
/**
 * Created by PhpStorm.
 * User: a21
 * Date: 2017/2/16
 * Time: 上午10:29
 */

class EmptyAction extends Action{
    public function index(){
        $paramName = MODULE_NAME;
        if (strtolower($paramName) == "autologin"){

            $code = $this->_get('code');
            print_r($code);
            $url = "http://103.36.173.184:8080/ecloud/oapi/token";
            $post_data = array ("grantType"=>"authorization_code",
                "code"=>$code,
                "redirectUri"=>"http://218.245.66.215/hrm/index.php/autologin",
                "clientId"=>"demohrm",
                "clientSecret"=>"demohrmsecret");
            $post_json = json_encode($post_data);
            $ch = curl_init();
            curl_setopt($ch, CURLOPT_URL, $url);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
            // post数据
            curl_setopt($ch, CURLOPT_POST, 1);
            // post的变量
            curl_setopt($ch, CURLOPT_POSTFIELDS, $post_json);
            curl_setopt($ch, CURLOPT_HTTPHEADER, array(
                'Content-Type: application/json',
                'Contnet-Length:'.strlen($post_json)
            ));
            $json = curl_exec($ch);//运行curl
            curl_close($ch);

            $data = json_decode(trim($json),true);
            if (isset($data['user']) && !empty($data['user'])) {
//                $d_user = D('User');
                $d_user = M('User');

                $user = $d_user->where(array('name' => $data['user']['username']))->find();

                if (empty($user['name'])) {
                    $userInfo['category_id'] = 0;
                    $userInfo['name'] = $data['user']['username'];
                    $userInfo['companyUserId'] = $data['user']['id'];
                    $userInfo['companyId'] = $data['user']['companyId'];
                    $d_user->add($userInfo);
                    $user = $d_user->where(array('name' => $data['user']['username']))->find();
                }


                if ($user['category_id'] == 1) {
                    session('admin', 1);
                }

                session('name', $data['user']['username']);
                session('user_id', $user['user_id']);
                session('accessToken', $data['accessToken']);

                redirect(U('core/index/index'));

            }else{
                alert('error','code已过期',U('core/user/login'));
            }
        }elseif (strtolower($paramName) == 'copyuserinfo'){
            //用户数据同步
            $url = "http://103.36.173.184:8080/ecloud/oapi/token";

            $post_data = array("grantType"=>"client_credentials",
                "clientId"=>"demohrm",
                 "clientSecret"=>"demohrmsecret");

            $post_json = json_encode($post_data);
            $ch = curl_init();
            curl_setopt($ch, CURLOPT_URL, $url);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
            curl_setopt($ch, CURLOPT_POST, 1);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $post_json);
            curl_setopt($ch, CURLOPT_HTTPHEADER, array(
                'Content-Type: application/json',
                'Contnet-Length:'.strlen($post_json)
            ));
            $infojson = curl_exec($ch);//运行curl
            curl_close($ch);

            $infodata = json_decode(trim($infojson),true);
            if (isset($infodata['accessToken']) && !empty($infodata['accessToken'])){
                $getstr = 'http://103.36.173.184:8080/ecloud/oapi/user?token='.$infodata['accessToken'];
                //初始化
                $ch = curl_init();
                //设置选项，包括URL
                curl_setopt($ch, CURLOPT_URL, $getstr);
                curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
                curl_setopt($ch, CURLOPT_HEADER, 0);
                //执行并获取HTML文档内容
                $output = curl_exec($ch);
                //释放curl句柄
                curl_close($ch);

                $userdata = json_decode(trim($output),true);

                if (isset($userdata['entity']) && !empty($userdata['entity'])){
                    $users = D('User');

                    foreach ($userdata['entity'] as $infodata){
                        $condition['name'] = $infodata['username'];
                        $count = $users->where($condition)->count();

                        $newdata['companyId'] = $infodata['companyId'];
                        $newdata['companyUserId'] = $infodata['companyUserId'];
                        $newdata['email'] = $infodata['email'];
                        $newdata['employeeName'] = $infodata['employeeName'];
                        $newdata['employeeNo'] = $infodata['employeeNo'];
                        $newdata['telephone'] = $infodata['phone'];
                        $newdata['name'] = $infodata['username'];

                        if ($count <= 0){
                            $newdata['status'] = 1;
                            $users->add($newdata);

                        }else{

                            $users->where($condition)->save($newdata);
                        }

                    }

                    print_r('用户信息同步成功');
                }else{
                    print_r('用户信息同步失败,user->entity为空');
                }
            }else{
                print_r('用户信息同步失败,token无效');
            }

        }elseif (strtolower($paramName) == 'copycompanyinfo'){
            //同步公司部门架构
            $url = "http://103.36.173.184:8080/ecloud/oapi/token";

            $post_data = array("grantType"=>"client_credentials",
                "clientId"=>"demohrm",
                "clientSecret"=>"demohrmsecret");

            $post_json = json_encode($post_data);
            $ch = curl_init();
            curl_setopt($ch, CURLOPT_URL, $url);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
            curl_setopt($ch, CURLOPT_POST, 1);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $post_json);
            curl_setopt($ch, CURLOPT_HTTPHEADER, array(
                'Content-Type: application/json',
                'Contnet-Length:'.strlen($post_json)
            ));
            $infojson = curl_exec($ch);//运行curl
            curl_close($ch);

            $infodata = json_decode(trim($infojson),true);
            if (isset($infodata['accessToken']) && !empty($infodata['accessToken'])){
                $getstr = 'http://103.36.173.184:8080/ecloud/oapi/department?token='.$infodata['accessToken'];
                //初始化
                $ch = curl_init();
                //设置选项，包括URL
                curl_setopt($ch, CURLOPT_URL, $getstr);
                curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
                curl_setopt($ch, CURLOPT_HEADER, 0);
                //执行并获取HTML文档内容
                $output = curl_exec($ch);
                //释放curl句柄
                curl_close($ch);

                $userdata = json_decode(trim($output),true);
                if (isset($userdata['entity']) && !empty($userdata['entity'])){

                    $this->updateCompany($userdata['entity']);

                    print_r('同步公司部门架构成功');
                }else{
                    print_r('同步公司部门架构失败,user->entity为空');
                }
            }else{
                print_r('同步公司部门架构失败,token无效');
            }

        }

    }


    /*递归遍历*/
    private function updateCompany($infoArr){

        $de_role = M('Department');

        $condition['department_id'] = $infoArr['departmentId'];
        $count = $de_role->where($condition)->count();

        $newdata['department_id'] = $infoArr['departmentId'];
        $newdata['parent_id'] = $infoArr['parentId'] ? $infoArr['parentId'] : 0;
        $newdata['name'] = $infoArr['nodeName'];

        if ($count <= 0){
            $de_role->add($newdata);
        }else{
            $de_role->where($condition)->save($newdata);
        }

        if (!is_array ($infoArr['children']) || empty($infoArr['children'])) return;

        foreach ($infoArr['children'] as $val )
        {

            $this->updateCompany($val);
        }
    }


}